package us.kg.kotoriforest.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import us.kg.kotoriforest.spring.condition.MacCondition;
import us.kg.kotoriforest.spring.condition.WindowsCondition;
import us.kg.kotoriforest.spring.pojo.User;

/**
 * @author Kotori rotroutine@163.com
 * @since 2025/1/24
 */
@Configuration
public class UserConfig {
    // 通过@Bean装配的对象其bean的名字为函数的名字 也可以通过@Bean注解的value属性来命名
    @Bean("zhangsan")
    public User zhangsan() {
        User user = new User();
        user.setName("张三");
        user.setAge(18);
        user.setGender("武装直升机");
        return user;
    }

    // @Scope 调整组件的作用域 默认为singleton单例模式 prototype为多例模式 request为同请求单例 session为同会话单例
    // @lazy 将组件设定为懒加载 只有在单例模式下有效
    @Bean("lisi")
    public User lisi() {
        return new User("李四", 22, "沃尔玛塑料袋");
    }

    @Conditional(WindowsCondition.class) // @Conditional注解可以根据条件来判断是否装配bean 不仅可以注释在方法上还可以注释在类上
    @Bean()
    public User Bill() {
        return new User("比尔盖茨", 66, "微软");
    }

    @Conditional(MacCondition.class)
    @Bean
    public User Jobs() {
        return new User("乔布斯", 66, "苹果");
    }

    @Bean
    public User LeiJun() {
        return new User("雷军", 51, "小米");
    }
}
